package com.athuawei.saleTicket;

class Tiket {
    private int number = 30;

    public synchronized void sale() {

        if(number > 0) {
            System.out.println(Thread.currentThread().getName() +
                    " : 卖出：" + number-- + "剩余：" + number);
        }
    }
}

public class SaleTicket {

    public static void main(String[] args) {

        Tiket tiket = new Tiket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 40; i++) {
                    tiket.sale();
                }
            }
        }, "AA").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 40; i++) {
                    tiket.sale();
                }
            }
        }, "BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 40; i++) {
                    tiket.sale();
                }
            }
        }, "CC").start();
    }
}
